package com.aclass.wang.c206;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.util.EventListener;

public class Main7Activity extends AppCompatActivity implements SensorEventListener {
//传感器
    SensorManager sensorManager;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main7);
        textView = (TextView) findViewById(R.id.text);
        sensorManager = (SensorManager)
                getSystemService(SENSOR_SERVICE);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_UI);
    }

    @Override
    protected void onStop() {
        super.onStop();
        sensorManager.unregisterListener(this);
    }


    @Override
    public void onSensorChanged(SensorEvent event) {
        float[] valus=event.values;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("方向角"+valus[0]+"/n");
        stringBuilder.append("俯仰角"+valus[1]+"/n");
        stringBuilder.append("翻转角"+valus[2]+"/n");
        textView.setText(stringBuilder.toString());
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}
